﻿--[[
   @brief    control for game_mgr
   @author   jasonlli
   @version  1.0
   @date     2015-03-16
--]]

tool_box_control =
{
    last_game_sel_ = {};
    init = function()
        ierd_tgp.tpf_ui.Require("tool_box\\view.lua");
        ierd_tgp.tpf_ui.Require("tool_box\\startup_speedup_control.lua");
        ierd_tgp.tpf_ui.Require("tool_box\\network_acc_control.lua");
        ierd_tgp.tpf_ui.Require("tool_box\\game_update_control.lua");
        ierd_tgp.tpf_ui.Require("tool_box\\hardware_detect_control.lua");
        event_center.add_listener(tool_box_event.TOOL_BOX_LOAD_PAGE,
                'tool_box_control.open_network_acc_page');
    end;

    close = function(uilib)
        local param = {};
        event_center.send_event(tool_box_event.TOOL_BOX_CLOSE_PAGE, base_utility.table_to_json(param));
        comm_center.call_service("Svr_SyncAssistInfo", {});
        tool_box_view.close(uilib);
        ierd_tgp.tpf_ui.Log("close tool_box window success.");
    end;


    init_game_base_info = function()
        tool_box_control.update_game_base_info();
        tool_box_model.game_base_info_ready = true;
    end;

    update_game_base_info = function()
        event_center.send_event(banner_event.UPDATE_GAME_BASE_INFO, "");
    end;

    lua_call_JS = function(func_name, json)
        local uilib = ui_utility.uilib();
        local nQBWebID = uilib:GetControlIDbyCustomID(CID_TOOLBOX_QBWEB);
        local pControl = uilib:GetControlPtr(nQBWebID);
        local pQBWeb = tolua.cast(pControl, "Tenio::IQBWebBrowser");
        local json_param = "TestTime = 456789";
        ierd_tgp.tpf_ui.Log("on_hostapp_callback('tool_box', '')");
        local const_func_name, str_func_name = U8toCUIStr("tool_box");
        local const_json_param, str_json_param = U8toCUIStr(json_param);
        ierd_tgp.tpf_ui.Log("lua_call_JS start.");
        if pQBWeb ~= nil then
            pQBWeb:ExcecuteJS(const_func_name, const_json_param);
        end
        ierd_tgp.tpf_ui.Log("lua_call_JS success.");
    end;

    on_js_callback = function(uilib, fuc_name, json)
        ierd_tgp.tpf_ui.Log("on_js_callback.fuc_name="..fuc_name);
        local control_id = uilib:GetControlIDbyCustomID(CID_TOOLBOX_QBWEB);
        local control = uilib:GetControlPtr(control_id);
        local component = tolua.cast(control, "Tenio::IQBWebBrowser");
        local const_func_name, str_func_name = U8toCUIStr(fuc_name);
        local const_ui_str, str_holder = U8toCUIStr(json);
        local fuc_suffix = string.sub(fuc_name, 1, 2);
        local str = {fuc_name = fuc_name, json = json};

        if fuc_name == 'tb_toolbox_ok' then
            tool_box_view.OnLoadOk();
            return;
        elseif fuc_name == "jc_close_window" then
            ui_utility.close_window({win_id=tool_box_model.rootId});
            return;
        end

        if fuc_suffix == "gm" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_GM, base_utility.table_to_json(str));
        elseif fuc_suffix == "st" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_ST, base_utility.table_to_json(str));
        elseif fuc_suffix == "na" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_NA, base_utility.table_to_json(str));
        elseif fuc_suffix == "se" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_SE, base_utility.table_to_json(str));
        elseif fuc_suffix == "gu" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_GU, base_utility.table_to_json(str));
        elseif fuc_suffix == "hc" then
            event_center.send_event(tool_box_event.TOOL_BOX_ON_JS_CALLBACK_HC, base_utility.table_to_json(str));
        elseif fuc_suffix == "toolbox" then
            web_utility.open_link(json);
        end;
    end;

    On_document_complete_callback = function(uilib, fuc_name, json)
        ierd_tgp.tpf_ui.Log("On_document_complete_callback.fuc_name="..fuc_name);
        local control_id = uilib:GetControlIDbyCustomID(CID_TOOLBOX_QBWEB);
        local control = uilib:GetControlPtr(control_id);
        local component = tolua.cast(control, "Tenio::IQBWebBrowser");
        local const_func_name, str_func_name = U8toCUIStr(fuc_name);
        local const_ui_str, str_holder = U8toCUIStr(json);
    end;

    open_tool_box_pages = function(event_id, param)
        local table_param = base_utility.json_to_table(param);
        if (table_param.type ~= 'tools') then
            return;
        end

        local page_name = table_param.sub_page;
        if page_name == "HOME" then
            tool_box_control.open_network_acc_page();
        elseif page_name == "NET_ACCE" then
            tool_box_control.open_network_acc_page();
        elseif page_name == "START_ACCE" then
            tool_box_control.open_startup_speedup_page();
        elseif page_name == "AUTO_UPDATE" then
            tool_box_control.open_game_update_page();
        elseif page_name == "HARDWARE_CHECK" then
            tool_box_control.open_hardware_check_page(table_param);
        elseif page_name == "CF_REPAIR" then
            event_center.send_event(tool_box_event.OPEN_CF_REPAIR_PAGE, param);
            return;
        elseif page_name == "LOL_REPAIR" then
            event_center.send_event(tool_box_event.OPEN_LOL_REPAIR_PAGE, param);
            return;
        end;

        local pdlg = ui_utility.get_ctrl_ptr_by_cid(CID_TOOLBOX_DLG);
        pdlg:BringToTop();
    end;

    -- 打开网络加速页面
    open_network_acc_page = function()
        local data_table = {};
        data_table.tab_id = 0;
        tool_box_view.open_toolbox_page(data_table);
    end;

    -- 打开启动加速页面
    open_startup_speedup_page = function()
        local data_table = {};
        data_table.tab_id = 1;
        tool_box_view.open_toolbox_page(data_table);
    end;

    -- 打开游戏更新页面
    open_game_update_page = function()
        local data_table = {};
        data_table.tab_id = 2;
        tool_box_view.open_toolbox_page(data_table);
    end;

    -- 打开硬件检测页面
    open_hardware_check_page = function(table_param)
        local data_table = {};
        data_table.tab_id = 3;

        local game_id = 26;   -- take LOL as default check game.
        if table_param.game_id ~= nil then
            game_id = table_param.game_id;
        end

        data_table.game_id = game_id;

        tool_box_view.open_toolbox_page(data_table);
    end;

    -- 显示游戏修复的游戏选择框
    on_event_open_repair_game_sel = function(event_ids, json)
        base_utility.log("[tool_box]on_event_open_repair_game_sel, " .. tostring(json));
        local in_param = base_utility.json_to_table(json);
        in_param.action = sel_action_type.kRepair;
        in_param.caption = LoadStr("ID_FIXGAME");

        tool_box_control.open_game_sel(in_param);
    end;

    -- 显示游戏卸载的游戏选择框
    on_event_open_uninstall_game_sel = function(event_ids, json)
        base_utility.log("[tool_box]on_event_open_uninstall_game_sel, " .. tostring(json));
        local in_param = base_utility.json_to_table(json);
        in_param.action = sel_action_type.kUninstall;
        in_param.caption = LoadStr("ID_UNISTALL");

        tool_box_control.open_game_sel(in_param);
    end;

    open_game_sel = function(in_param)
        tool_box_control.last_game_sel_ = in_param;

        local param = {
            url = repair_game_model.game_sel_url;
            use_ptlogin = false;
            width = 620;
            height = 352;
            caption = in_param.caption;
            show_loading_page = false;
            region_set_zero = true;
            reload = true;
            title_in_web = true;
        };
        comm_center.call_service("i_service_show_simple_web_window", param);
    end;

    on_service_get_attach_game_sel_items = function(data)
        base_utility.log("[tool_box]on_service_get_attach_game_sel_items");
        game_id = tool_box_control.last_game_sel_.game_id;

        local action = tool_box_control.last_game_sel_.action;
        local attach_info = game_utility.get_attach_rel(game_id);
        local main_game = tool_box_model.get_game_sel_item(attach_info.main_game_id, action);
        local sub_game = tool_box_model.get_game_sel_item(attach_info.sub_game_id, action);

        main_game.is_main = true;
        sub_game.is_main = false;

        data.action = tool_box_control.last_game_sel_.action;
        data.items = {main_game, sub_game};

        return data;
    end;
};

tool_box_control.init();
